package yxy.game.pm2.command.pack;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.levy;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.coin.CoinBean;
import yxy.game.pm2.module.coin.CoinModule;

/**
 * 背包 兑换钱币界面
 */
@MessageType(proto = Command.CoinConversionInfo, classtype = PackageMessage.CoinConversionInfoRequest.class)
public class CMD_CoinConversionInfo extends Command<PackageMessage.CoinConversionInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, PackageMessage.CoinConversionInfoRequest request) {
        String playerCode = messageSource.getTag();

        PackageMessage.CoinConversionInfoResponse.Builder response = PackageMessage.CoinConversionInfoResponse.newBuilder();

        CoinModule coinModule = CoinModule.get(playerCode);
        for (CoinBean bean : coinModule.getCoinBeans()) {
            PackageMessage.CoinConversion.Builder builder = PackageMessage.CoinConversion.newBuilder();
            builder.setDataId(bean.getDataId());
            builder.setTimes(bean.times());

            //返回银币数量
            levy levy = DataCenter.getData(bean.getDataId(), yxy.cherry.data.bean.levy.class);
            //根据小时返回的银币数量
            long coinNum = CoinBean.getSilver(playerCode, levy.getTime());
            //计算vip加成后的银币数量
            long totalNum = CoinBean.vipRewardCoinNum(playerCode, coinNum);
            builder.setCoinNum(totalNum);
            response.addCc(builder);
        }
        response.setRefresh(coinModule.refreshTime());

        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, PackageMessage.CoinConversionInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}